package main

import (
	"crypto/md5"
	"encoding/hex"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
	"time"
)

type User struct {
	User string
	Pass string
	Url  string
}

// 获取到md5
func GetMd5(data string) string {

	// 创建一个新的MD5 hash实例
	hash := md5.New()

	// 将字符串写入hash
	hash.Write([]byte(data))

	// 计算MD5签名并转换为十六进制字符串
	return hex.EncodeToString(hash.Sum(nil))
}

// 获取签名信息
func GetSign(user User) string {
	now := time.Now()
	dateStr := now.Format("20060102")
	hour := now.Hour()
	min := now.Minute()
	signStr := fmt.Sprintf("u=%s&t=%s&h=%d&m=%d&p=%s&u=%s", user.User, dateStr,hour,min, user.Pass, user.User)
	fmt.Println("签名字符串signStr:",signStr)
	sign := GetMd5(signStr)
	return sign
}

func main() {

	// 获取命令行参数
	args := os.Args

	fmt.Println("启动代码仓库ip")
	// 打包时记得写这几个即可
	user := User{
		User: "", // 用户名
		Pass: "", // 密码
		Url:  "", // 请求url
	}

	url := user.Url

	fmt.Println("当前用户：", user.User);

	sign := GetSign(user)

	realUrl := url + "?user=" + user.User + "&sign=" + sign
	if len(args) > 1 {
		realUrl += "&type=1"	
		fmt.Println("临时ip：", args[1])
	}
	// fmt.Println("请求url：", realUrl)

	resp, err := http.Get(realUrl)
	if err != nil {
		fmt.Println("Error making GET request:", err)
		return
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Error reading response body:", err)
		return
	}

	type BodyInfo struct {
		Message string `json:"message"`
	}
	var bodyData BodyInfo
	json.Unmarshal(body, &bodyData)
	fmt.Println("Response:", string(body))
	fmt.Println("Response:", bodyData.Message)
	time.Sleep(time.Second * 5)
}
